//    Openbravo POS is a point of sales application designed for touch screens.
//    Copyright (C) 2007-2009 Openbravo, S.L.
//    http://www.openbravo.com/product/pos
//
//    This file is part of Openbravo POS.
//
//    Openbravo POS is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Openbravo POS is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Openbravo POS.  If not, see <http://www.gnu.org/licenses/>.

package com.openbravo.pos.inventory;

import cn.ekuma.data.dao.I_DataLogic;
import cn.ekuma.data.ui.swing.SwingEditorRecord;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.module.ERP_UI_Swing_Model;
import cn.ekuma.epos.panel.JPanelTableDAO;
import cn.ekuma.epos.productsfinishing.ProductsEditor;

import java.awt.Component;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.LocalRes;
import com.openbravo.data.user.EditorListener;
import com.openbravo.data.user.I_BrowsableListEditableData;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.base.AppView;
import com.openbravo.bean.SharedProduct;
import com.openbravo.pos.bean.ProductExt;
import com.openbravo.pos.forms.JRootApp;
import com.openbravo.pos.panels.ProductFilter;
import java.awt.event.ActionListener;
import javax.swing.JPanel;

/**
 *
 * @author adrianromero
 * Created on 1 de marzo de 2007, 22:15
 *
 */
public class ProductsPanel extends JPanelTableDAO<ProductExt> implements EditorListener {

    private ProductsEditor jeditor;
    private ProductFilter jproductfilter;    
    
    private I_DataLogicERP m_dlSales;
    
    /** Creates a new instance of ProductsPanel2 */
    public ProductsPanel() {
    }
    
    protected void init() {   
        m_dlSales = mode.getDLERP();
        
        // el panel del filtro
        jproductfilter = new ProductFilter(m_dlSales);
        
        // el panel del editor
        jeditor = new ProductsEditor(m_dlSales, dirty);
        jproductfilter.setBarCodeListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                   bd.actionLoad();
                } catch (BasicException eD) {
                 MessageInf msg = new MessageInf(MessageInf.SGN_NOTICE, LocalRes.getIntString("message.noreload"), eD);
                 msg.show(ProductsPanel.this);
              }
            }

        });
    }
    
    public SwingEditorRecord getEditor() {
        return jeditor;
    }
    
    @Override
    public Component getFilter() {
        return jproductfilter.getComponent();
    }  
    
    @Override
    public Component getToolbarExtras() {
        JPanel extToolbar=new JPanel();
        extToolbar.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
        JButton btnScanPal = new JButton();
        btnScanPal.setText("ScanPal");
        btnScanPal.setVisible(((JRootApp) app).getDeviceScanner() != null);
        btnScanPal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnScanPalActionPerformed(evt);
            }
        });
        extToolbar.add(btnScanPal);
        JButton btnAddShared = new JButton();
        btnAddShared.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/ark216.png"))); // NOI18N
        btnAddShared.setFocusable(false);
        btnAddShared.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	ProductExt obj=(ProductExt) bd.getCurrentElement();
               if(obj!=null){
                    try {
                        m_dlSales.saveSharedProduct(new SharedProduct(obj.getID()));
                    } catch (BasicException ex) {
                      new MessageInf(ex).show(ProductsPanel.this);
                    }
               }
            }
        });
        extToolbar.add(btnAddShared);
        return extToolbar;
    }
    
    private void btnScanPalActionPerformed(java.awt.event.ActionEvent evt) {                                           
        JDlgUploadProducts.showMessage(this, ((JRootApp) app).getDeviceScanner(), (I_BrowsableListEditableData) bd);
    }  
    
    public String getTitle() {
        return AppLocal.getIntString("Menu.Products");
    } 
        
    @Override
    public void activate() throws BasicException {
        
        jeditor.activate(); 
        jproductfilter.activate();

        startNavigation();
        //super.activate();
    } 
    
    public void updateValue(Object value) {
    }

    @Override
    public I_DataLogic getDataLogic() {
        return m_dlSales;
    }

    @Override
    public Class getSuportClass() {
        return ProductExt.class;
    }

    @Override
    public String getObjectRenderString(ProductExt value) {
        return value.getReference()+"-"+value.getName();
    }

	@Override
	public String getModelName() {
		return ERP_UI_Swing_Model.MODEL_NAME;
	}
}
